﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;

namespace NetTopologySuite.Noding
{
	public class SegmentIntersectionDetector : ISegmentIntersector
	{
		public SegmentIntersectionDetector() : this(new RobustLineIntersector())
		{
		}

		public SegmentIntersectionDetector(LineIntersector li)
		{
			this._li = li;
		}

		public bool FindProper { get; set; }

		public bool FindAllIntersectionTypes { get; set; }

		public bool HasIntersection
		{
			get
			{
				return this._hasIntersection;
			}
		}

		public bool HasProperIntersection
		{
			get
			{
				return this._hasProperIntersection;
			}
		}

		public bool HasNonProperIntersection
		{
			get
			{
				return this._hasNonProperIntersection;
			}
		}

		public Coordinate Intersection
		{
			get
			{
				return this._intPt;
			}
		}

		public Coordinate[] IntersectionSegments
		{
			get
			{
				return this._intSegments;
			}
		}

		public void ProcessIntersections(ISegmentString e0, int segIndex0, ISegmentString e1, int segIndex1)
		{
			if (e0 == e1 && segIndex0 == segIndex1)
			{
				return;
			}
			Coordinate[] coordinates = e0.Coordinates;
			Coordinate coordinate = coordinates[segIndex0];
			Coordinate coordinate2 = coordinates[segIndex0 + 1];
			Coordinate[] coordinates2 = e1.Coordinates;
			Coordinate coordinate3 = coordinates2[segIndex1];
			Coordinate coordinate4 = coordinates2[segIndex1 + 1];
			this._li.ComputeIntersection(coordinate, coordinate2, coordinate3, coordinate4);
			if (this._li.HasIntersection)
			{
				this._hasIntersection = true;
				bool isProper = this._li.IsProper;
				if (isProper)
				{
					this._hasProperIntersection = true;
				}
				if (!isProper)
				{
					this._hasNonProperIntersection = true;
				}
				bool flag = !this.FindProper || isProper;
				if (this._intPt == null || flag)
				{
					this._intPt = this._li.GetIntersection(0);
					this._intSegments = new Coordinate[4];
					this._intSegments[0] = coordinate;
					this._intSegments[1] = coordinate2;
					this._intSegments[2] = coordinate3;
					this._intSegments[3] = coordinate4;
				}
			}
		}

		public bool IsDone
		{
			get
			{
				if (this.FindAllIntersectionTypes)
				{
					return this._hasProperIntersection && this._hasNonProperIntersection;
				}
				if (this.FindProper)
				{
					return this._hasProperIntersection;
				}
				return this._hasIntersection;
			}
		}

		private readonly LineIntersector _li;

		private bool _hasIntersection;

		private bool _hasProperIntersection;

		private bool _hasNonProperIntersection;

		private Coordinate _intPt;

		private Coordinate[] _intSegments;
	}
}
